{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "__title__      = ML in Action Chapter9 Code(Tkinter GUI)\n",
    "__author__     = wgj\n",
    "__createDate__ = 2018-11-8 13:32:35"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from tkinter import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\"\"\" 测试Tkinter GUI \"\"\"\n",
    "root = Tk()\n",
    "myLable = Label(root,text=\"Hello Wordl\")\n",
    "myLable.grid()\n",
    "root.mainloop()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "from numpy import *\n",
    "import regTrees\n",
    "\n",
    "import matplotlib\n",
    "matplotlib.use('TkAgg')  # 设置matplotlib后端为TkAgg\n",
    "from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg\n",
    "from matplotlib.figure import Figure\n",
    "\n",
    "\"\"\" 根据输入框设置，重绘图形 \"\"\"\n",
    "def reDraw(tolS,tolN):\n",
    "    reDraw.f.clf()        # 清除原有图形\n",
    "    reDraw.a = reDraw.f.add_subplot(111)  # 重新添加子图\n",
    "    if chkBtnVar.get():   # 检查复选框是否被选中(选中则构建模型树)\n",
    "        if tolN < 2: tolN = 2   \n",
    "        myTree=regTrees.createTree(reDraw.rawDat, regTrees.modelLeaf,\\\n",
    "                                   regTrees.modelErr, (tolS,tolN))\n",
    "        yHat = regTrees.createForeCast(myTree, reDraw.testDat, \\\n",
    "                                       regTrees.modelTreeEval)\n",
    "    else:                 # 构建回归树\n",
    "        myTree=regTrees.createTree(reDraw.rawDat, ops=(tolS,tolN))\n",
    "        yHat = regTrees.createForeCast(myTree, reDraw.testDat)\n",
    "    reDraw.a.scatter(reDraw.rawDat[:,0].A, reDraw.rawDat[:,1].A, s=5)  # 使用散点图绘制真实值\n",
    "    reDraw.a.plot(reDraw.testDat, yHat, linewidth=2.0)             # 使用曲线图绘制预测值\n",
    "    reDraw.canvas.show()\n",
    "  \n",
    "\"\"\" 获取输入框内容 \"\"\"\n",
    "def getInputs():\n",
    "    try: tolN = int(tolNentry.get())\n",
    "    except:  # 无法将输入转换为int型时进行如下异常处理\n",
    "        tolN = 10 \n",
    "        print(\"enter Integer for tolN\")\n",
    "        tolNentry.delete(0, END)\n",
    "        tolNentry.insert(0,'10')\n",
    "    try: tolS = float(tolSentry.get())\n",
    "    except:  # 无法将输入转换为float型时进行如下异常处理\n",
    "        tolS = 1.0 \n",
    "        print(\"enter Float for tolS\")\n",
    "        tolSentry.delete(0, END)\n",
    "        tolSentry.insert(0,'1.0')\n",
    "    return tolN,tolS"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "测试基于Tkinter的树回归"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "D:\\Anaconda3\\lib\\site-packages\\ipykernel_launcher.py:10: MatplotlibDeprecationWarning: The FigureCanvasTk.show function was deprecated in version 2.2. Use FigureCanvasTk.draw instead.\n",
      "  # Remove the CWD from sys.path while we load stuff.\n",
      "D:\\Anaconda3\\lib\\site-packages\\ipykernel_launcher.py:22: MatplotlibDeprecationWarning: The FigureCanvasTk.show function was deprecated in version 2.2. Use FigureCanvasTk.draw instead.\n"
     ]
    }
   ],
   "source": [
    "\"\"\" 绘制新树 \"\"\"\n",
    "def drawNewTree():\n",
    "    tolN,tolS = getInputs()  # 获取文本框的内容\n",
    "    reDraw(tolS,tolN)\n",
    "    \n",
    "root=Tk()    # 使用Tkinter创建根部件\n",
    "\n",
    "reDraw.f = Figure(figsize=(5,4), dpi=100) # 创建画布\n",
    "reDraw.canvas = FigureCanvasTkAgg(reDraw.f, master=root)  # 与matplotlib链接\n",
    "reDraw.canvas.show()\n",
    "reDraw.canvas.get_tk_widget().grid(row=0, columnspan=3)\n",
    "\n",
    "Label(root, text=\"tolN\").grid(row=1, column=0)\n",
    "tolNentry = Entry(root)\n",
    "tolNentry.grid(row=1, column=1)\n",
    "tolNentry.insert(0,'10')\n",
    "Label(root, text=\"tolS\").grid(row=2, column=0)\n",
    "tolSentry = Entry(root)\n",
    "tolSentry.grid(row=2, column=1)\n",
    "tolSentry.insert(0,'1.0')\n",
    "Button(root, text=\"ReDraw\", command=drawNewTree).grid(row=1, column=2, rowspan=3)\n",
    "chkBtnVar = IntVar()\n",
    "chkBtn = Checkbutton(root, text=\"Model Tree\", variable = chkBtnVar)\n",
    "chkBtn.grid(row=3, column=0, columnspan=2)\n",
    "\n",
    "reDraw.rawDat = mat(regTrees.loadDataSet('sine.txt'))\n",
    "reDraw.testDat = arange(min(reDraw.rawDat[:,0]),max(reDraw.rawDat[:,0]),0.01)\n",
    "reDraw(1.0, 10)\n",
    "               \n",
    "root.mainloop()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
